package com.le.tester.javaThink.fourth.concurrency;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * createTime：2021/10/22 16:04
 * description：store在不同位置的调用情况
 */

class ExplicitManager1 extends PairManager {

    private Lock lock = new ReentrantLock();

    @Override
    public synchronized void increment() {
        lock.lock();
        try {
            p.incrementX();
            p.incrementY();
            store(getPair());
        } finally {
            lock.unlock();
        }
    }
}

class ExplicitPairManager2 extends PairManager {

    private Lock lock = new ReentrantLock();


    @Override
    public void increment() {
        Pair temp;
        lock.lock();
        try {
            p.incrementX();
            p.incrementY();
            temp = getPair();
        } finally {
            lock.unlock();
        }
        store(temp);
    }
}

public class ExplicitCriticalSection {
    public static void main(String[] args) {
        PairManager manager1 = new ExplicitManager1();
        PairManager manager2 = new ExplicitPairManager2();
        CriticalSection.testApproaches(manager1, manager2);
    }
}
